// customheaderview.cpp
#include "customheaderview.h"
#include <QPainter>
#include <QIcon>
#include <QFontMetrics>
#include <QTextDocument>
#include <QTextCharFormat>
#include <QFont>
#include <QColor>
#include <QTextCursor>

CustomHeaderView::CustomHeaderView(Qt::Orientation orientation, QWidget *parent)
    : QHeaderView(orientation, parent)
{
    m_font = QFont("Microsoft YaHei", 11, QFont::Bold);
    m_textColor = Qt::white; // 设置字体颜色为白色
    m_BackgroundColor = Qt::lightGray;
}

void CustomHeaderView::setHeaderData(const QMap<int, QVariant>& data)
{
    m_headerData = data;
    updateGeometries(); // 更新几何信息以反映新的表头数据
}



void CustomHeaderView::setColumnWidths(const QMap<int, int> &widths)
{
    m_columnWidths = widths;
    updateGeometries(); // 更新几何信息以反映新的列宽
}

void CustomHeaderView::setSectionHeight(int logicalIndex, int height)
{
    m_manualHeights[logicalIndex] = height;
    updateGeometries();
}

void CustomHeaderView::setBackgroundColor(const QColor &color)
{
    m_BackgroundColor = color;
    update();
}

void CustomHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    painter->save();

    // 绘制背景色
    painter->fillRect(rect, m_BackgroundColor);

    // 设置表头字体
    painter->setFont(m_font);
    painter->setPen(m_textColor);
    // 获取表头内容
    QVariant data = m_headerData.value(logicalIndex);
    if (data.isValid()) {
        QString text = data.toMap().value("text").toString();
        QString iconPath = data.toMap().value("icon").toString();

        QIcon icon(iconPath);
        QPixmap pixmap = icon.pixmap(20); // 图标大小为16x16


        // 计算图标和文本的中心位置
        int iconWidth = pixmap.width();
        int iconHeight = pixmap.height();
        int textWidth = rect.width();
        int textHeight = QFontMetrics(m_font).height();

        // 计算总高度
        int totalHeight = iconHeight + textHeight + 10; // 10是间距
        // 计算中心位置
        int xCenter = rect.center().x();
        int yCenter = rect.center().y();


        int iconX = xCenter - iconWidth  / 2;
        int iconY = 6;//yCenter - totalHeight / 2;

        // 计算图片的位置
        //int iconX = rect.left() + 5; // 5是间距
        //int iconY = rect.top() + 5; // 5是间距
        painter->drawPixmap(iconX, iconY, pixmap);

        // 计算文本的位置
        int textX = xCenter - textWidth / 2;
        int textY = iconY + iconHeight; // 10是间距

        // 使用 QTextDocument 处理多行文本
        QTextDocument doc;

        doc.setHtml(text); // 支持 HTML 格式的文本
        doc.setDefaultFont(m_font);
        doc.setTextWidth(textWidth);
        doc.setDefaultTextOption(QTextOption(Qt::AlignCenter)); // 居中对齐

        // 设置文本颜色
        QTextCharFormat format;
        format.setForeground(m_textColor);
        QTextCursor cursor(&doc);
        cursor.select(QTextCursor::Document);
        cursor.mergeCharFormat(format);

        // 绘制文本
        painter->translate(textX, textY);
        doc.drawContents(painter);
        painter->translate(-textX, -textY);
        //painter->drawText(textRect, Qt::AlignCenter, text);

        // 新增绘制白色竖线的逻辑
        if (logicalIndex < this->count() - 1) { // 确保不是最后一个section
            painter->setPen(Qt::white); // 设置画笔颜色为白色
            painter->drawLine(rect.right(), rect.top(), rect.right(), rect.bottom()); // 绘制竖线
        }
    } else {
        // 如果没有自定义数据，使用默认的表头内容
        QStyleOptionHeader option;
        initStyleOption(&option);
        style()->drawControl(QStyle::CE_HeaderSection, &option, painter, this);

        QString text = model()->headerData(logicalIndex, orientation(), Qt::DisplayRole).toString();
        option.text = text;
        style()->drawControl(QStyle::CE_HeaderLabel, &option, painter, this);
    }

    painter->restore();
}

QSize CustomHeaderView::sectionSizeFromContents(int logicalIndex) const
{
    QVariant data = m_headerData.value(logicalIndex);
    if (data.isValid()) {
        QString text = data.toMap().value("text").toString();
        QString iconPath = data.toMap().value("icon").toString();

        int columnWidth = m_columnWidths.value(logicalIndex, 150); // 默认宽度为150

        // 检查是否有手动设置的高度
        if (m_manualHeights.contains(logicalIndex)) {
            return QSize(columnWidth, m_manualHeights[logicalIndex]);
        }

        // 计算文本的高度
        QTextDocument doc;
        doc.setHtml(text); // 支持 HTML 格式的文本
        doc.setDefaultFont(m_font);
        doc.setTextWidth(columnWidth);


        int textHeight = doc.size().height();
        int iconHeight = 20; // 图标的高度
        int totalHeight = textHeight + iconHeight + 20; // 20是总间距
//        QFontMetrics fontMetrics(m_font);
//        int textHeight = fontMetrics.boundingRect(text).height();
//        int iconHeight = 16; // 图标的高度
//        int totalHeight = textHeight + iconHeight + 10; // 10是间距

        return QSize(columnWidth, totalHeight);
    } else {
        // 如果没有自定义数据，使用默认的表头内容
        QStyleOptionHeader option;
        initStyleOption(&option);
        return style()->sizeFromContents(QStyle::CT_HeaderSection, &option, QSize(), this);
    }
}
